<?php
/*
	插件概览
*/
defined('BY_JYA') or exit('error');
$merchid=$this->checklogin();
$_RQ['op']=$_RQ['op']?$_RQ['op']:'get';
if($_RQ['op']=='get'){
	$item=array();
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'set',
		'desc'=>'自定义导出',
		'edit'=>array(
			'0'=>array('type'=>'string','title'=>'订单号','name'=>'ordersn'),
			'1'=>array('type'=>'radio','title'=>'订单状态','name'=>'status','options'=>'status'),
			'2'=>array('type'=>'radio','title'=>'支付方式','name'=>'paytype','options'=>'paytype'),
			'3'=>array('type'=>'checkbox','title'=>'自定义导出数据','name'=>'diy','options'=>'diy'),
		)
	);
	$return['paytype']=array('1'=>'微信支付','2'=>'余额支付');
	$return['status']=array('1'=>'待发货','2'=>'待收货','3'=>'已完成','4'=>'已关闭');
	$return['diy']=array('ordersn'=>'订单号','nickname'=>'买家昵称','phone'=>'买家电话','mid'=>'会员id','address'=>'收货地址','realname'=>'收货人','mobile'=>'收货电话','goods'=>'商品信息','paytype'=>'支付方式','goodsprice'=>'商品小计','discountprice'=>'运费','createtime'=>'下单时间','paytime'=>'付款时间','sendtime'=>'发货时间','finishtime'=>'订单完成时间','express'=>'快递公司','expresssn'=>'快递单号','remark'=>'订单备注');
	$return['htmls']=$htmls;
	
	exi($return);
}
if($_RQ['op']=='set'){
	$post=get_postdata();
	
	
	$_SESSION['e2cpost']=$post;
	$url=download_url('jy_weishop/order.export/down');
	exi('点确定开始下载','','',$url);
	
}
if($_RQ['op']=='down'){
	$f=array('ordersn'=>'订单号','nickname'=>'买家昵称','phone'=>'买家电话','mid'=>'会员id','address'=>'收货地址','realname'=>'收货人','mobile'=>'收货电话','goods'=>'商品信息','paytype'=>'支付方式','goodsprice'=>'商品小计','discountprice'=>'运费','createtime'=>'下单时间','paytime'=>'付款时间','sendtime'=>'发货时间','finishtime'=>'订单完成时间','express'=>'快递公司','expresssn'=>'快递单号','remark'=>'订单备注');
	$diy=explode(';',$_SESSION['e2cpost']['diy']);
	$header=array();
	foreach($diy as $v){
		if($v){
		$header[$v]=$f[$v];
		}
	}
	$params=array('plugin_name'=>'jy_mfuwu');
	if(!empty($_SESSION['e2cpost']['ordersn'])){
		$params['ordersn']=trim($_SESSION['e2cpost']['ordersn']);
	}
	if(!empty($_SESSION['e2cpost']['status'])){
		$params['status']=intval($_SESSION['e2cpost']['status']);
	}
	if(!empty($_SESSION['e2cpost']['paytype'])){
		$params['paytype']=intval($_SESSION['e2cpost']['paytype']);
	}
	//创建时间
	if($_SESSION['e2cpost']['createtime']){
		//$time_range=cfc('tools')->base64_decode($_SESSION['e2cpost']['createtime'],$_SESSION['base_key_str']);
		//$time_range=json_decode($time_range,true);
		$time_range=$_SESSION['e2cpost']['createtime'];
		$params['createtime between']=array($time_range['tstart'],$time_range['tend']);
	}
	$params['uniacid']=$_SESSION['uniacid'];
	$params['merchid']=$merchid;
	$data=pdo_getall('jy_weishop_order',$params);
	
	$status=array('1'=>'待发货','2'=>'待收货','3'=>'已完成','4'=>'已关闭');
	$paytype=array('1'=>'微信支付','2'=>'余额支付');
	$ispay=array('0'=>'未支付','1'=>'已支付');
	foreach($data as &$v){
		$v['nickname']=pdo_getcolumn('core_members',array('id'=>$v['mid']),'nickname');
		$v['phone']=pdo_getcolumn('core_members',array('id'=>$v['mid']),'mobile');			
		$address=pdo_get('jy_weishop_address',array('id'=>$v['addressid'],'uniacid'=>$_SESSION['uniacid']));
		$v['address']=$address['province'].$address['city'].$address['address'];
		$v['mobile']=$address['mobile'];
		$v['realname']=$address['realname'];
		$q=$v['status'];
		$v['status']=$status[$q];
		$w=$v['paytype'];
		$v['paytype']=$paytype[$w];
		$e=$v['ispay'];
		$v['ispay']=$ispay[$e];
		if(!$v['nickname']){
			$v['nickname']='匿名用户';
		}
		$v['createtime']=date('Y-m-d H:i:s',$v['createtime']);
		$v['payime']=date('Y-m-d H:i:s',$v['payime']);
		$v['sendtime']=date('Y-m-d H:i:s',$v['sendtime']);
		$v['finishtime']=date('Y-m-d H:i:s',$v['finishtime']);
		$v['storetitle']=pdo_getcolumn('jy_weishop_store',array('id'=>$v['storeid']),'title');
		$v['storetitle']=$v['storetitle']?:'无';
		$goods=pdo_getall('jy_weishop_order_goods',array('orderid'=>$v['id']),'goodsid');
		foreach($goods as $k=>$gv){
			$goodstitle=pdo_getcolumn('jy_weishop_goods',array('id'=>$gv['goodsid']),'title');
			$num=$k+1;
			$v['goods'] .='【商品'.$num.'】'.$goodstitle;
		}
	}
	cfc('tools')->export2excel($header,$data,'订单');
}